Learn to Code HTML & CSS 10
Lesson 10: Building Forms
Contents
- How to initialize a form
- Ways to obtain text-based information from users
- Different elements and methods for creating multiple choice options and menus
- Which elements and attributes are best used to submit a form’s data for processing
- How best to organize forms and give form controls structure and meaning
- A handful of attributes that help collect more qualified data
Forms are an essential part of the Internet, as they provide a way for websites to capture information from users and to process requests, and they offer controls for nearly every imaginable use of an application. Through controls or fields, forms can request a small amount of information—often a search query or a username and password—or a large amount of information—perhaps shipping and billing information or an entire job application.
Initializing a Form
To add a form to a page, we’ll use the <form
> element. The <form
> element identifies where on the page control elements will appear. Additionally, the <form
> element will wrap all of the elements included within the form, much like a <div
> element.
|
|
A handful of different attributes can be applied to the <form
> element, the most common of which are action and method. The action attribute contains the URL to which information included within the form will be sent for processing by the server. The method attribute is the HTTP method browsers should use to submit the form data. Both of these <form
> attributes pertain to submitting and processing data.
Text Fields & Textareas
When it comes to gathering text input from users, there are a few different elements available for obtaining data within forms. Specifically, text fields and textareas are used for collecting text- or string-based data. This data may include passages of text content, passwords, telephone numbers, and other information.
Text Fields
One of the primary elements used to obtain text from users is the <input
> element. The <input
> element uses the type attribute to define what type of information is to be captured within the control. The most popular type attribute value is text, which denotes a single line of text input.
Along with setting a type attribute, it is best practice to give an <input
> element a name attribute as well. The name attribute value is used as the name of the control and is submitted along with the input data to the server.
|
|
The <input
> element is self-contained, meaning it uses only one tag and it does not wrap any other content. The value of the element is provided by its attributes and their corresponding values.
Originally, the only two text-based type attribute values were text and password (for password inputs); however, HTML5 brought along a handful of new type attribute values.
|
|
These values were added to provide clearer semantic meaning for inputs as well as to provide better controls for users. Should a browser not understand one of these HTML5 type attribute values, it will automatically fall back to the text attribute value. Below is a list of the new HTML5 input types.
- color
- date
- datetime
- month
- number
- range
- search
- tel
- time
- url
- week
Textarea
Another element that’s used to capture text-based data is the <textarea
> element. The <textarea
> element differs from the <input
> element in that it can accept larger passages of text spanning multiple lines. The <textarea
> element also has start and end tags that can wrap plain text. Because the <textarea
> element only accepts one type of value, the type attribute doesn’t apply here, but the name attribute is still used.
|
|
The <textarea
> element has two sizing attributes: cols for width in terms of the average character width and rows for height in terms of the number of lines of visible text. The size of a textarea, however, is more commonly identified using the width and height properties within CSS.
Multiple Choice Inputs & Menus
Apart from text-based input controls, HTML also allows users to select data using multiple choice and drop-down lists. There are a few different options and elements for these form controls, each of which has distinctive benefits.
Radio Buttons
Radio buttons are an easy way to allow users to make a quick choice from a small list of options. Radio buttons permit users to select one option only, as opposed to multiple options.
To create a radio button, the <input
> element is used with a type attribute value of radio. Each radio button element should have the same name attribute value so that all of the buttons within a group correspond to one another. Also, we have to define the input value. Using the value attribute, we can set a specific value for each <input
> element. Additionally, to preselect a radio button for users we can use the Boolean attribute checked.
|
|
Check Boxes
Check boxes are very similar to radio buttons. They use the same attributes and patterns, with the exception of checkbox as their type attribute value. The difference between the two is that check boxes allow users to select multiple values and tie them all to one control name, while radio buttons limit users to one value.
|
|
Drop-Down Lists
Drop-down lists are a perfect way to provide users with a long list of options in a practical manner. To create a drop-down list we’ll use the <select
> and <option
> elements. The <select
> element wraps all of the menu options, and each menu option is marked up using the <option
> element.
|
|
Multiple Selections
The Boolean attribute multiple, when added to the <select
> element for a standard drop-down list, allows a user to choose more than one option from the list at a time. Additionally, using the selected Boolean attribute on more than one <option
> element within the menu will preselect multiple options.
The size of the <select
> element can be controlled using CSS and should be adjusted appropriately to allow for multiple selections. It may be worthwhile to inform users that to choose multiple options they will need to hold down the Shift key while clicking to make their selections.
|
|
Form Buttons
After a user inputs the requested information, buttons allow the user to put that information into action. Most commonly, a submit input or submit button is used to process the data.
Submit Input
Users click the submit button to process data after filling out a form. The submit button is created using the <input
> element with a type attribute value of submit. The value attribute is used to specify the text that appears within the button.
|
|
Submit Button
As an <input
> element, the submit button is self-contained and cannot wrap any other content. If more control over the structure and design of the input is desired—along with the ability to wrap other elements—the <button
> element may be used.
The <button
> element performs the same way as the <input
> element with the type attribute value of submit; however, it includes opening and closing tags, which may wrap other elements. By default, the <button
> element acts as if it has a type attribute value of submit, so the type attribute and value may be omitted from the <button
> element if you wish.
|
|
Other Inputs
Besides the applications we’ve just discussed, the <input
> element has a few other use cases. These include passing hidden data and attaching files during form processing.
Hidden Input
Hidden inputs provide a way to pass data to the server without displaying it to users. Hidden inputs are typically used for tracking codes, keys, or other information that is not pertinent to the user but is helpful when processing the form. This information is not displayed on the page; however, it can be found by viewing the source code of a page. It should therefore not be used for sensitive or secure information.
|
|
File Input
To allow users to add a file to a form, much like attaching a file to an email, use the file value for the type attribute.
|
|
Unfortunately, styling an <input
> element that has a type attribute value of file is a tough task with CSS. Each browser has its own default input style, and none provide much control to override the default styling. JavaScript and other solutions can be employed to allow for file input, but they are slightly more difficult to construct.
Organizing Form Elements
Knowing how to capture data with inputs is half the battle. Organizing form elements and controls in a usable manner is the other half. When interacting with forms, users need to understand what is being asked of them and how to provide the requested information.
By using labels, fieldsets, and legends, we can better organize forms and guide users to properly complete them.
Label
Labels provide captions or headings for form controls, unambiguously tying them together and creating an accessible form for all users and assistive technologies. Created using the <label
> element, labels should include text that describes the inputs or controls they pertain to.
Labels may include a for attribute. The value of the for attribute should be the same as the value of the id attribute on the form control the label corresponds to. Matching up the for and id attribute values ties the two elements together, allowing users to click on the <label
> element to bring focus to the proper form control.
|
|
If desired, the <label
> element may wrap form controls, such as radio buttons or check boxes. Doing so allows omission of the for and id attributes.
|
|
Fieldset
Fieldsets group form controls and labels into organized sections. Much like a <section
> or other structural element, the <fieldset
> is a block-level element that wraps related elements, specifically within a <form
> element, for better organization. Fieldsets, by default, also include a border outline, which can be modified using CSS.
|
|
Legend
A legend provides a caption, or heading, for the <fieldset
> element. The <legend
> element wraps text describing the form controls that fall within the fieldset. The markup should include the <legend
> element directly after the opening <fieldset
> tag. On the page, the legend will appear within the top left part of the fieldset border.
|
|
Form & Input Attributes
To accommodate all of the different form, input, and control elements, there are a number of attributes and corresponding values. These attributes and values serve a handful of different functions, such as disabling controls and adding form validation.
Disabled
The disabled Boolean attribute turns off an element or control so that it is not available for interaction or input. Elements that are disabled will not send any value to the server for form processing.
|
|
Placeholder
The placeholder HTML5 attribute provides a hint or tip within the form control of an <input
> or <textarea
> element that disappears once the control is clicked in or gains focus. This is used to give users further information on how the form input should be filled in, for example, the email address format to use.
|
|
The main difference between the placeholder and value attributes is that the value attribute value text stays in place when a control has focus unless a user manually deletes it. This is great for pre-populating data, such as personal information, for a user but not for providing suggestions.
Required
The required HTML5 Boolean attribute enforces that an element or form control must contain a value upon being submitted to the server. Should an element or form control not have a value, an error message will be displayed requesting that the user complete the required field. Currently, error message styles are controlled by the browser and cannot be styled with CSS. Invalid elements and form controls, on the other hand, can be styled using the :optional and :required CSS pseudo-classes.
Validation also occurs specific to a control’s type. For example, an <input
> element with a type attribute value of email will require not only that a value exist within the control, but also that it is a valid email address.
|
|
Additional Attributes
Other form and form control attributes include, but are not limited to, the following. Please feel free to research these attributes as necessary.
- accept
- autocomplete
- autofocus
- formaction
- formenctype
- formmethod
- formnovalidate
- formtarget
- max
- maxlength
- min
- pattern
- readonly
- selectionDirection
- step
Login Form Example
The following is an example of a complete login form that includes several different elements and attributes to illustrate what we’ve covered so far. These elements are then styled using CSS.
|
|
|
|
Summary
Forms play a large role in how users interact with, provide information to, and take action on websites. We’ve taken all the right steps to learn not only how to mark up forms but also how to style them.
To quickly recap, within this lesson we discussed the following:
- How to initialize a form
- Ways to obtain text-based information from users
- Different elements and methods for creating multiple choice options and menus
- Which elements and attributes are best used to submit a form’s data for processing
- How best to organize forms and give form controls structure and meaning
- A handful of attributes that help collect more qualified data